import type { NextAuthOptions } from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import Credentials from 'next-auth/providers/credentials'
import prismaDb from "./prismadb";
import {compare} from 'bcrypt'
import GitHubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
// import EmailProvider from "next-auth/providers/email";

export const authOptions = {
  adapter: PrismaAdapter(prismaDb) as any,
  providers: [
    Credentials({
      id:"credentials",
      name:"Credentials",
      credentials: {
        email: { label: "Email", type: "text", placeholder: "Enter your email" },
        password: { label: "Password", type: "password", placeholder: "Enter your password" },
      },
      authorize: async (credentials, req) => {
        if(!credentials?.email || !credentials?.password){
          throw new Error("Invalid email or password");
        }
        const user = await prismaDb.user.findUnique({
          where: {
            email: credentials?.email,
          }
        })
        if (!user|| !user.hashedPassword) {
          throw new Error("Email does not exist");
        }

        const isValidPassword = await compare(
          credentials?.password,
          user.hashedPassword
        )
        if (!isValidPassword) {
          throw new Error("Invalid email or password");
        }
        return user
      },
    }),
    
    GitHubProvider({
      clientId: process.env.GITHUB_ID as string,
      clientSecret: process.env.GITHUB_SECRET as string,
    }),
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID as string,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
     
    }),
    // EmailProvider({
    //   server: {
    //     host: process.env.EMAIL_SERVER_HOST,
    //     port: process.env.EMAIL_SERVER_PORT,
    //     auth: {
    //       user: process.env.EMAIL_SERVER_USER,
    //       pass: process.env.EMAIL_SERVER_PASSWORD,
    //     },
    //   },
    //   from: process.env.EMAIL_FROM,
    // }),
  ],
  pages:{
    signIn: '/auth',
  },
  debug: process.env.NODE_ENV === "development",
  session:{
    strategy: "jwt",
  },
  jwt:{
    secret: process.env.NEXTAUTH_JWT_SECRET,
  },
  secret: process.env.NEXTAUTH_SECRET,
} satisfies NextAuthOptions
